define(function () {
    'use strict';
    function ctrl($scope, $state, LocalCacheService, WebService, CommonService,TdxLocalFuncService) {
        var isClick=true;
        function init() {
            $scope.user = LocalCacheService.getUser();
            
            var busineState=$scope.user.busineState;
            var errorMsg='';
            if (busineState=='-1') {
                errorMsg = '当前业务操作不可用[-1]，如有疑问请联系在线客服或客服电话！';
            }else if(busineState=='1') {
                errorMsg='您尚未绑定手机号[1]，请携带身份证件到附近营业部绑定，若有疑问请联系在线客服或客服电话！';
            }else if(busineState=='2') {
                errorMsg='您尚未绑定手机号[2]，请携带身份证件到附近营业部绑定。若已绑定，请联系在线客服或客服电话！';
            }else if(busineState=='3') {
                errorMsg='您客户号绑定的手机号与开户时注册的手机号不一致[3]，如有疑问请联系在线客服或客服电话！';
            }else if(busineState=='4') {
                errorMsg='您客户号绑定的手机号与开户时注册的手机号不一致[4]，如有疑问请联系在线客服或客服电话！';
            }else if(busineState=='5') {
                errorMsg='您尚未绑定手机号[5]，请携带身份证件到附近营业部绑定，若有疑问请联系在线客服或客服电话！';
            }else if(busineState=='6') {
                errorMsg='您尚未绑定手机号[6]，请携带身份证件到附近营业部绑定，若有疑问请联系在线客服或客服电话！';
            }
            if(busineState!=0){
                CommonService.showConfig({message:errorMsg});
                $state.go('tab.index');
                return;
            }
            
            $scope.userAccounts = [{title: '沪A股票开户', value: '', href: 'tab.account-openAccount', param:1, isOpen: false},
                {title: '深A股票开户', value: '', href: 'tab.account-openAccount', param:2, isOpen: false},
                {title: '沪市TA基金开户', value: '', href: 'tab.account-openta', param:1, isOpen: false},
                {title: '深市TA基金开户', value: '', href: 'tab.account-openta', param:2, isOpen: false},
                {title: '创业板转签', value: '', href: 'tab.account-entrep', param:null, isOpen: false},
                {title: '沪港通开户', value: '', href: 'tab.account-OpenStocksHK', param:1, isOpen: false},
                {title: '深港通开户', value: '', href: 'tab.account-OpenStocksHK', param:2, isOpen: false},
                //{title: '易融通', value: '', href: '#/tab/account-easyFinancing', isOpen: true},
                {title: '签署电子签名约定书', value: '', href: null, param:null, isOpen: false}];
            var sparams = {client_id: $scope.user.client_id};
            WebService.qryStockholder(sparams).then(function (data) {
                $scope.userAccounts[0].isOpen = true;
                $scope.userAccounts[1].isOpen = true;
                $scope.userAccounts[4].isOpen = true;
                if ((data.H_A.open_status == '1' || data.H_A.open_date != null) && data.H_A.regflag == '1') {//已开通
                    $scope.userAccounts[0].value = '已开通';
                }
                if (data.S_A.open_status == '1' || data.S_A.open_date != null) {//已开通
                    $scope.userAccounts[1].value = '已开通';
                    if (data.S_A.holder_rights.indexOf("j") != -1) {
                        $scope.userAccounts[4].value = '已开通';
                    }
                }
            });
            var oparams = {client_id: $scope.user.client_id, fund_account: $scope.user.fund_account};
            WebService.qryOfStockHolderAccount(oparams).then(function (data) {
                $scope.userAccounts[2].isOpen = true;
                $scope.userAccounts[3].isOpen = true;
                if (data.H_TA.open_status == '1') {//已开通
                    $scope.userAccounts[2].value = '已开通';
                }
                if (data.S_TA.open_status == '1') {//已开通
                    $scope.userAccounts[3].value = '已开通';
                }
            });
            //查询港股通状态
            $scope.userAccounts[5].isOpen = true;
            $scope.userAccounts[5].value = '';
            $scope.userAccounts[6].isOpen = true;
            $scope.userAccounts[6].value = '';
            /*var yparams = {client_id: $scope.user.client_id, fund_account: $scope.user.fund_account};
             FinancingService.getIndexInfo(yparams).then(function (data) {
             $scope.userAccounts[5].isOpen = true;
             if (data.error_no!= '-1') {//已开通
             $scope.userAccounts[5].href = '#/tab/account-easyFinancingHome';
             $scope.userAccounts[5].value = '已开通';
             }
             });*/
        }

        $scope.go=function(url){
            if(isClick){
                $state.go(url);
            }else if(!LocalCacheService.getUser()){
                $state.go(url);
            }
        };

        $scope.onBackKeyDown = function () {
            if(isClick){
                if (TdxLocalFuncService.isTDX()) {
                    TdxLocalFuncService.tdxTradeLocked().then(function (data) {
                        if (data.text == '1' || data.text==1) {
                            return;
                        } else {
                            $state.go('tab.account-assets-index');
                        }
                    });
                }else{
                    $state.go('tab.account-assets-index');
                }
            }else if(!LocalCacheService.getUser()){
                $state.go('tab.index');
            }
        };

        $scope.$on('$ionicView.beforeEnter', function () {
            if(TdxLocalFuncService.isTDX()){
                TdxLocalFuncService.checkUser(init);
            }else{
                init();
            }
        });
        $scope.eSign = function () {
            if (TdxLocalFuncService.isTDX()) {
                TdxLocalFuncService.openDzht().then(function (data) {
                });
            }
        };
        $scope.set = function () {
            if (TdxLocalFuncService.isTDX()) {
                isClick=false;
                TdxLocalFuncService.sysSet().then(function () {
                    TdxLocalFuncService.checkUser(function(){
                        isClick=true;
                        init();
                    });
                });
            }
        };
    }

    ctrl.$inject = ['$scope', '$state', 'LocalCacheService', 'WebService','CommonService', 'TdxLocalFuncService'];
    return ctrl;
});
